package com.event.web.controller.system;

import com.event.common.annotation.Log;
import com.event.common.constant.UserConstants;
import com.event.common.core.controller.BaseController;
import com.event.common.core.domain.AjaxResult;
import com.event.common.core.page.TableDataInfo;
import com.event.common.core.text.Convert;
import com.event.common.enums.BusinessType;
import com.event.system.domain.SysEventRoster;
import com.event.system.domain.SysEventSort;
import com.event.system.service.ISysEventRosterService;
import com.event.system.service.ISysEventSortService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lw
 */
@Controller
@RequestMapping("/system/eventsort")
public class SysEventSortController extends BaseController {

    private String prefix = "system/eventsort";

    @Autowired
    private ISysEventSortService sysEventSortService;

    @Autowired
    private ISysEventRosterService sysEventRosterService;

    @RequiresPermissions("system:eventsort:view")
    @GetMapping()
    public String eventsort()
    {
        return prefix + "/eventsort";
    }

    @RequiresPermissions("system:eventsort:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysEventSort sysEventSort)
    {
        startPage();
        List<SysEventSort> list = sysEventSortService.selectSortEventSortList(sysEventSort);
        return getDataTable(list);
    }

    /**
     * 新增展会分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存展会分类
     */
    @RequiresPermissions("system:eventsort:add")
    @Log(title = "展会分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysEventSort sysEventSort)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(sysEventSortService.checkSortNameUnique(sysEventSort)))
        {
            return error("新增展会分类'" + sysEventSort.getSortName() + "'失败，展会分类名称已存在");
        }
        return toAjax(sysEventSortService.add(sysEventSort));

    }

    /**
     * 校验展会分类名称
     */
    @PostMapping("/checkSortNameUnique")
    @ResponseBody
    public String checkSortNameUnique(SysEventSort sysEventSort)
    {
        return sysEventSortService.checkSortNameUnique(sysEventSort);
    }

    /**
     * 修改分类
     */
    @RequiresPermissions("system:eventsort:edit")
    @GetMapping("/edit/{sortId}")
    public String edit(@PathVariable("sortId") Long sortId, ModelMap mmap)
    {
        mmap.put("eventSort", sysEventSortService.selectSortEventSortBySortId(sortId));
        return prefix + "/edit";
    }

    /**
     * 修改保存分类
     */
    @RequiresPermissions("system:eventsort:edit")
    @Log(title = "展会分类管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysEventSort sysEventSort)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(sysEventSortService.checkSortNameUnique(sysEventSort)))
        {
            return error("修改展会分类'" + sysEventSort.getSortName() + "'失败，展会分类名称已存在");
        }
        return toAjax(sysEventSortService.update(sysEventSort));
    }

    /**
     * 删除展会分类
     */
    @RequiresPermissions("system:eventsort:remove")
    @Log(title = "展会分类管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysEventSortService.deleteSysEventSortBySortId(ids));
    }
}
